/**
* @Title: grading_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年10月29日 下午2:22:48
* @version V1.0
*/
;
(function(){
	'use strict';
	var app = angular.module('fwa.mainApp');
	app.controller('GradingQueryCtrl', ['$scope','$state','$modal','BaseURL','Modal','GradingService','InsuranceGradingService',
	                                function(scope,$state,$modal,baseUrl,Modal,GradingService,InsuranceGradingService){
		scope.gradingService = GradingService;
		//tabs集合
		scope.tabs =[
		             {"name": "医疗机构评分标准","currentTab": "1"},
		             {"name": "医保服务人员评分标准","currentTab": "2"},
		             {"name": "药店评分标准","currentTab": "3"}
		             ];
		
		//点击tab事件
		scope.tabClick = function(data) {
			scope.currentTab = data.currentTab;
			queryGradingData();
		}
		
		var orgGrading = "gradingOther.orgGradingOther",
			insurance  = "gradingOther.insuranceOther",
			drugstore = "gradingOther.drugstoreOther";
		//
		function  queryGradingData (){
			if (scope.currentTab == 1) {
				GradingService.insuranceGradingData ={};
				$state.go(orgGrading);
			}
			else if (scope.currentTab == 2) {
				GradingService.insuranceGradingData ={};
				$state.go(insurance);
			}
			else if (scope.currentTab == 3) {
				GradingService.insuranceGradingData ={};
				$state.go(drugstore);
			}
		}
		
		function selectedTab() {
			if($state.current.name 
			   && $state.current.name === insurance) {
				//选中当前tab
				scope.currentTab = scope.tabs[1].currentTab;	
			}
			else if ($state.current.name 
					   && $state.current.name === drugstore) {
				//选中当前tab
				scope.currentTab = scope.tabs[2].currentTab;
			}
			else {
				//选中当前tab
				scope.currentTab = scope.tabs[0].currentTab;
			}
			queryGradingData();
		}
		
		selectedTab();
		
		//新增评分标准
		scope.addGrading = function() {
			if (scope.currentTab == 1) {
				orgGradingAdd();
				GradingService.insuranceType = 'add';
				GradingService.orgType = '';
			}
			else if (scope.currentTab == 2) {
				insuranceGradingAdd();
				GradingService.insuranceType = 'add';
				GradingService.insuranceType = '';
			}
			else if (scope.currentTab == 3) {
				drugstoreGradingAdd();
				GradingService.insuranceType = 'add';
				GradingService.drugstoreType = '';
			}
		}
		//修改评分标准
		scope.updateGrading = function(type){
			if (scope.currentTab == 1) {
				if (GradingService.insuranceGradingData 
				    && GradingService.insuranceGradingData.standardscoreid) {
					orgGradingAdd();
					GradingService.insuranceType = 'update';
				}
				else {
					Modal.alert("请选择一个医疗机构评分标准");
				}
			}
			else if (scope.currentTab == 2) {
				if (GradingService.insuranceGradingData 
				    && GradingService.insuranceGradingData.standardscoreid) {
						insuranceGradingAdd();
						GradingService.insuranceType = 'update';
				}
				else {
					Modal.alert("请选择一个医保服务人员评分标准");
				}
			}
			else if (scope.currentTab == 3) {
				if (GradingService.insuranceGradingData 
				    && GradingService.insuranceGradingData.standardscoreid) {
					drugstoreGradingAdd();
					GradingService.insuranceType = 'update';
				}
				else {
					Modal.alert("请选择一个药店评分标准");
				}
			}
		}
		
		//删除
		scope.deleteGrading = function(){
			var id = GradingService.insuranceGradingData.standardscoreid;
			if(!id){
				Modal.alert("请选择一个评分标准");
				return;
			}else{
				Modal.confirm("确定删除 [" + GradingService.insuranceGradingData.scoreName + "] 评分标准吗?",function(){
					GradingService.loading = true;
					GradingService.deleteGrading(id).success(function(res){
						if(res.state==0){
							InsuranceGradingService.queryInsuranceGradingData(scope.currentTab-1);
							Modal.alert("删除成功！");
						}else{
							Modal.alert(res.mdg);
						}
						GradingService.loading = false;
					});
				});
			}
		}
		
		//医疗机构 评分标准
		function orgGradingAdd(){
			var orgGradingAddModal = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/agreementQuery/grading/orgGrading_add.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller: 'OrgGradingAddOrUpdateQueryCtrl'
		      	});
		}
		
		//医保服务人员  新增修改操作
		function insuranceGradingAdd(){
			var insuranceGradingAddModal = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/agreementQuery/grading/insuranceGrading_add.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller:'InsuranceGradingAddOrUpdateQueryCtrl'
		      	});
		}
		
		//药店 新增以及修改
		function drugstoreGradingAdd(){
			var drugstoreGradingAddModal = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/agreementQuery/grading/drugstoreGrading_add.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller: 'DrugstoreGradingAddOrUpdateQueryCtrl'
		      	});
		}
		
	}]);
})();